package api

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
	"service_base_shop_api/user_web/forms"
	"strconv"

	"context"
	"service_base_shop_api/user_web/global"
	protopb "service_base_shop_api/user_web/proto"
	"service_base_shop_api/user_web/utils"
)

//获取
func AddrList(ctx *gin.Context)  {
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id,_ := id.(uint64)
	UserAddrList,err := global.AddressClient.GetUserAddr(context.Background(),&protopb.GetUserAddrListReq{ID: int64(user_id)})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
		return
	}
	addr_lsit := []gin.H{}
	json.Unmarshal([]byte(UserAddrList.AddrList),&addr_lsit)
	ctx.JSON(http.StatusOK,gin.H{"status":1,"msg":"","adds":addr_lsit})
}

//设置默认值
func SetDefault(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"addr_id"})
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id,_ := id.(uint64)
	addr_id,_ := strconv.Atoi(parms["addr_id"])
	_,err := global.AddressClient.SetDefault(context.Background(),&protopb.SetDefaultReq{AddrID: int64(addr_id),UserID: int64(user_id)})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
	}
	ctx.JSON(http.StatusOK,gin.H{"status":1,"msg":""})
}

func DelAddr(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"addr_id"})
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id,_ := id.(uint64)
	addr_id,_ := strconv.Atoi(parms["addr_id"])
	_,err := global.AddressClient.DelAddr(context.Background(),&protopb.DelReq{AddrID: int64(addr_id),UserID: int64(user_id)})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
	}
	ctx.JSON(http.StatusOK,gin.H{"status":1,"msg":""})
}

func GetProvince(ctx *gin.Context)  {
	GetProvinceAddr,err :=global.AddressClient.GetProvince(context.Background(),&protopb.GetProvinceAddrList{})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
		return
	}
	addr_list := []gin.H{}
	err = json.Unmarshal([]byte(GetProvinceAddr.AddrList),&addr_list)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{
			"msg": "解析错误",
			"status":0,
		})
	}
	ctx.JSON(http.StatusOK,gin.H{"list":addr_list,"status":1})

}

func GetCity(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"sheng"})
	sheng_id,_ := strconv.Atoi(parms["sheng"])
	GetCityAddr,err :=global.AddressClient.GetCityAddr(context.Background(),&protopb.GetCityAddrList{ShengId: int64(sheng_id)})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
		return
	}
	sheng := GetCityAddr.Id
	addr_list := []gin.H{}
	err = json.Unmarshal([]byte(GetCityAddr.AddrList),&addr_list)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{
			"msg": "解析错误",
			"status":0,
		})
	}
	ctx.JSON(http.StatusOK,gin.H{"city_list":addr_list,"status":1,"sheng":sheng})
}

func GetArea(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"city","sheng"})
	sheng_id,_ := strconv.Atoi(parms["sheng"])
	city_id,_ := strconv.Atoi(parms["city"])
	GetAreaAddr,err :=global.AddressClient.GetAreaAddr(context.Background(),&protopb.GetAreaAddrList{ShengId: int64(sheng_id),CityId: int64(city_id)})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
		return
	}
	city := GetAreaAddr.Id
	addr_list := []gin.H{}
	err = json.Unmarshal([]byte(GetAreaAddr.AddrList),&addr_list)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{
			"msg": "解析错误",
			"status":0,
		})
	}
	ctx.JSON(http.StatusOK,gin.H{"area_list":addr_list,"status":1,"city":city})
}

func AddAdds(ctx *gin.Context)  {
	addrfrom := forms.AddAddrForm{}
	if err := ctx.ShouldBind(&addrfrom); err != nil{
		utils.HandleValidatorError(ctx,err)
		return
	}
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id := id.(uint64)
	addr_list,err := global.AddressClient.AddAddr(context.Background(),&protopb.Addr{
		Uid: int64(user_id),
		Tel: addrfrom.Tel,
		City: addrfrom.City,
		Sheng: addrfrom.Sheng,
		Quyu: addrfrom.Quyu,
		Name: addrfrom.Receiver,
		Address: addrfrom.Adds,
		Code: addrfrom.Code,
	})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
		return
	}
	addr_list_json := gin.H{}
	err = json.Unmarshal([]byte(addr_list.AddAddr),&addr_list_json)
	ctx.JSON(http.StatusOK,gin.H{"add_arr":addr_list_json,"status":1})

}


func GetChrtList(ctx *gin.Context)  {
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})

	}
	user_id := id.(uint64)

	list,err := global.UserSrvClient.ChrtList(context.Background(),&protopb.ChrtListReq{UserId: int64(user_id)})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK,gin.H{"cart":list.List,"status":1})

}

func UpdataCart(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"num","cart_id"})
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id := id.(uint64)
	cart_id,_ := strconv.Atoi(parms["cart_id"])
	num,_ := strconv.Atoi(parms["num"])
	_,err := global.UserSrvClient.UpdataCart(context.Background(),&protopb.UpdataCartReq{UserId: int64(user_id),CartId: int64(cart_id),Num: int64(num)})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK,gin.H{"succ":"操作成功!","status":1})

}

func DelCart(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"cart_id"})
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id := id.(uint64)
	cart_id,_ := strconv.Atoi(parms["cart_id"])
	_,err := global.UserSrvClient.DelCart(context.Background(),&protopb.DelCartReq{CartId: int64(cart_id),UserId: int64(user_id)})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err, ctx)
	}
	ctx.JSON(http.StatusOK,gin.H{"succ":"操作成功!","status":1})
}

func AddChrt(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"pid","num"})
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id := id.(uint64)
	pid,_ := strconv.Atoi(parms["pid"])
	num,_ := strconv.Atoi(parms["num"])

	cart,err := global.UserSrvClient.AddChrt(context.Background(),&protopb.AddChrtReq{Num: int64(num),Pid: int64(pid),Id: int64(user_id)})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK,gin.H{"cart_id":cart.CartId,"status":1})
}

func CollGoods(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"pid"})
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id := id.(uint64)
	pid,_ := strconv.Atoi(parms["pid"])
	_,err := global.UserSrvClient.CollGoods(context.Background(),&protopb.CollGoodsReq{UserId: int64(user_id),Pid: int64(pid)})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK,gin.H{"status":1})
}

func GetVoucher(ctx *gin.Context)  {
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id := id.(uint64)
	data,err := global.UserSrvClient.GetVoucher(context.Background(),&protopb.GetVoucherReq{
		UserId: int64(user_id),
	})
	if err != nil {
		//utils.HandleGrpcErrorToHttp(err, ctx)
		ctx.JSON(http.StatusOK,gin.H{"status":1,"offdates":[]string{},"nouses":[]string{},"useds":[]string{}})
		return
	}
	ctx.JSON(http.StatusOK,gin.H{"status":1,"offdates":data.Offdates,"nouses":data.Nouses,"useds":data.Useds})
}